#include "stm32f10x.h"                  // Device header

int16_t EnconderCount;
void Enconder_Init( )
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);  //GPIO口配置
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	GPIO_InitTypeDef GPIO_Initstructure;
	GPIO_Initstructure.GPIO_Mode =GPIO_Mode_IPU;
	GPIO_Initstructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
	GPIO_Initstructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_Initstructure);
    
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource1);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource2);
	
	EXTI_InitTypeDef EXTI_InitStructure;
	EXTI_InitStructure.EXTI_Line = EXTI_Line1 | EXTI_Line2;  //PA1,PA2
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;  //中断功能
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;   //下降沿触发
	EXTI_Init(&EXTI_InitStructure);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef  NVIC_InitStructure;
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;   //中断1接口
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   //
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;   //响应优先级
    NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;   //中断2接口
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   //
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;   //响应优先级
    NVIC_Init(&NVIC_InitStructure);
}
int16_t Getcount( )
{
	int16_t Temp;
	Temp = EnconderCount;
	EnconderCount = 0;
	return Temp;
}

void EXTI1_IRQHandler( )
{
	if(EXTI_GetITStatus(EXTI_Line1) == SET)
	{
		 if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2) == 0)
		 {
			 EnconderCount++;
		 }
		 	EXTI_ClearITPendingBit(EXTI_Line1);
	}
}
void EXTI2_IRQHandler( )
{
	if(EXTI_GetITStatus(EXTI_Line2) == SET)
	{
		if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == 0)
		 {
			 EnconderCount--;
	
		 }
		 	EXTI_ClearITPendingBit(EXTI_Line2);
	}
}